package com.fone.player.view;

import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.fone.player.ApplicationManage;
import com.fone.player.R;
import com.fone.player.entity.Download;
import com.fone.player.storage.SharedPreferenceModule;
import com.fone.player.storage.download.DownloadAppManager;
import com.fone.player.storage.download.NotificationHandle;
import com.fone.player.util.ScreenUtil;

@SuppressWarnings("unused")
public class WeixinDownloadDialog implements OnClickListener {

	private static final String TAG = "UpdateDialog";

	public static final String WEIXIN_DOWNLOAD_URL = "weixin_download_url";

	public static final int SUGGEST = 0;
	public static final int FORCE = 1;

	private TextView mTitle, mDesc;
	private CustomDialog dialog;
	private Context mContext;
	private int updateFlag;

	public WeixinDownloadDialog(Context mContext, Activity activity) {
		this.mContext = mContext;
		View updateView = LayoutInflater.from(mContext).inflate(
				R.layout.personal_update_dialog, null);
		dialog = new CustomDialog((Context) activity, updateView);
		initDialogView(mContext, updateView);

	}

	private void initDialogView(Context mContext, View updateView) {
		dialog.setCanceledOnTouchOutside(false);
		dialog.show();
		if (getScreenOrientation() == Configuration.ORIENTATION_PORTRAIT) {
			updateView.setMinimumWidth((int) (ScreenUtil
					.getScreenWidthPix(mContext) * 0.8));// 设置dialog的宽度
		} else {
			updateView.setMinimumWidth((int) (ScreenUtil
					.getScreenWidthPix(mContext) * 0.5));// 设置dialog的宽度
		}
		mTitle = (TextView) updateView.findViewById(R.id.update_title);
		mTitle.setText("提示");
		mDesc = (TextView) updateView.findViewById(R.id.update_content_tv);
		mDesc.setText("您需要下载微信客户端。");
		Button changeSureBtn = (Button) updateView
				.findViewById(R.id.update_sure_id);
		changeSureBtn.setText("下载");
		changeSureBtn.setOnClickListener(this);

		Button changeCancelBtn = (Button) updateView
				.findViewById(R.id.update_cancel_id);
		changeCancelBtn.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.update_sure_id:

			String downloadUrl = SharedPreferenceModule.getInstance()
					.getString(WeixinDownloadDialog.WEIXIN_DOWNLOAD_URL);
			if (TextUtils.isEmpty(downloadUrl)
					|| !downloadUrl.startsWith("http://")) {
				downloadUrl = mContext
						.getString(R.string.weixin_default_download_url);
			}

			Download download = new Download();
			download.setDownloadIsShowRunningNotification(true);
			download.setDownloadUrl(downloadUrl);
			download.setDownloadFileName("微信");
			download.setDownloadNotification(ApplicationManage
					.getGlobalContext());
			download.setDownloadIsInstall(true);
			download.setDownloadIsErrorToast(true);
			download.setDownloadIsLimitSpeed(false);
			download.setDownloadType(Download.DOWNLOAD_RECOMMEND);

			NotificationHandle.init(mContext);
			DownloadAppManager.getInstance().addDownloadFile(download);

			dialog.cancel();
			break;
		case R.id.update_cancel_id:
			dialog.cancel();
			break;
		}
	}

	/**
	 * 获取屏幕方向
	 * 
	 * @return
	 * @return int
	 * @throws
	 */
	private int getScreenOrientation() {
		return mContext.getResources().getConfiguration().orientation;
	}
}
